<!DOCTYPE HTML>
<html lang="zh-CN">


<head>
    <meta charset="utf-8">
    <meta name="keywords" content="MybatisPlus, Kevin&#39;s Blog">
    <meta name="description" content="">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
    <meta name="renderer" content="webkit|ie-stand|ie-comp">
    <meta name="mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
    <!-- Global site tag (gtag.js) - Google Analytics -->


    <title>MybatisPlus | Kevin&#39;s Blog</title>
    <link rel="icon" type="image/png" href="/icon.png">

    <link rel="stylesheet" type="text/css" href="/libs/awesome/css/all.css">
    <link rel="stylesheet" type="text/css" href="/libs/materialize/materialize.min.css">
    <link rel="stylesheet" type="text/css" href="/libs/aos/aos.css">
    <link rel="stylesheet" type="text/css" href="/libs/animate/animate.min.css">
    <link rel="stylesheet" type="text/css" href="/libs/lightGallery/css/lightgallery.min.css">
    <link rel="stylesheet" type="text/css" href="/css/matery.css">
    <link rel="stylesheet" type="text/css" href="/css/my.css">

    <script src="/libs/jquery/jquery.min.js"></script>

<meta name="generator" content="Hexo 5.4.0"></head>




<body>
    <header class="navbar-fixed">
    <nav id="headNav" class="bg-color nav-transparent">
        <div id="navContainer" class="nav-wrapper container">
            <div class="brand-logo">
                <a href="/" class="waves-effect waves-light">
                    
                    <img src="/icon.png" class="logo-img" alt="LOGO">
                    
                    <span class="logo-span">Kevin&#39;s Blog</span>
                </a>
            </div>
            

<a href="#" data-target="mobile-nav" class="sidenav-trigger button-collapse"><i class="fas fa-bars"></i></a>
<ul class="right nav-menu">
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/" class="waves-effect waves-light">
      
      <i class="fas fa-home" style="zoom: 0.6;"></i>
      
      <span>博客主页</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/archives" class="waves-effect waves-light">
      
      <i class="fas fa-archive" style="zoom: 0.6;"></i>
      
      <span>博客归档</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/about" class="waves-effect waves-light">
      
      <i class="fas fa-user-circle" style="zoom: 0.6;"></i>
      
      <span>关于Kevin</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/friends" class="waves-effect waves-light">
      
      <i class="fas fa-address-book" style="zoom: 0.6;"></i>
      
      <span>查看大佬</span>
    </a>
    
  </li>
  
  <li>
    <a href="#searchModal" class="modal-trigger waves-effect waves-light">
      <i id="searchIcon" class="fas fa-search" title="搜索" style="zoom: 0.85;"></i>
    </a>
  </li>
</ul>


<div id="mobile-nav" class="side-nav sidenav">

    <div class="mobile-head bg-color">
        
        <img src="/icon.png" class="logo-img circle responsive-img">
        
        <div class="logo-name">Kevin&#39;s Blog</div>
        <div class="logo-desc">
            
            Never really desperate, only the lost of the soul.
            
        </div>
    </div>

    

    <ul class="menu-list mobile-menu-list">
        
        <li class="m-nav-item">
	  
		<a href="/" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-home"></i>
			
			博客主页
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/archives" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-archive"></i>
			
			博客归档
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/about" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-user-circle"></i>
			
			关于Kevin
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/friends" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-address-book"></i>
			
			查看大佬
		</a>
          
        </li>
        
        
        <li><div class="divider"></div></li>
        <li>
            <a href="https://github.com/ZKQevin" class="waves-effect waves-light" target="_blank">
                <i class="fab fa-github-square fa-fw"></i>Fork Me
            </a>
        </li>
        
    </ul>
</div>


        </div>

        
            <style>
    .nav-transparent .github-corner {
        display: none !important;
    }

    .github-corner {
        position: absolute;
        z-index: 10;
        top: 0;
        right: 0;
        border: 0;
        transform: scale(1.1);
    }

    .github-corner svg {
        color: #0f9d58;
        fill: #fff;
        height: 64px;
        width: 64px;
    }

    .github-corner:hover .octo-arm {
        animation: a 0.56s ease-in-out;
    }

    .github-corner .octo-arm {
        animation: none;
    }

    @keyframes a {
        0%,
        to {
            transform: rotate(0);
        }
        20%,
        60% {
            transform: rotate(-25deg);
        }
        40%,
        80% {
            transform: rotate(10deg);
        }
    }
</style>

<a href="https://github.com/ZKQevin" class="github-corner tooltipped hide-on-med-and-down" target="_blank"
   data-tooltip="Fork Me" data-position="left" data-delay="50">
    <svg viewBox="0 0 250 250" aria-hidden="true">
        <path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path>
        <path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2"
              fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path>
        <path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z"
              fill="currentColor" class="octo-body"></path>
    </svg>
</a>
        
    </nav>

</header>

    



<div class="bg-cover pd-header post-cover" style="background-image: url('/medias/featureimages/21.jpg')">
    <div class="container" style="right: 0px;left: 0px;">
        <div class="row">
            <div class="col s12 m12 l12">
                <div class="brand">
                    <h1 class="description center-align post-title">MybatisPlus</h1>
                </div>
            </div>
        </div>
    </div>
</div>




<main class="post-container content">

    
    <link rel="stylesheet" href="/libs/tocbot/tocbot.css">
<style>
    #articleContent h1::before,
    #articleContent h2::before,
    #articleContent h3::before,
    #articleContent h4::before,
    #articleContent h5::before,
    #articleContent h6::before {
        display: block;
        content: " ";
        height: 100px;
        margin-top: -100px;
        visibility: hidden;
    }

    #articleContent :focus {
        outline: none;
    }

    .toc-fixed {
        position: fixed;
        top: 64px;
    }

    .toc-widget {
        width: 345px;
        padding-left: 20px;
    }

    .toc-widget .toc-title {
        padding: 35px 0 15px 17px;
        font-size: 1.5rem;
        font-weight: bold;
        line-height: 1.5rem;
    }

    .toc-widget ol {
        padding: 0;
        list-style: none;
    }

    #toc-content {
        padding-bottom: 30px;
        overflow: auto;
    }

    #toc-content ol {
        padding-left: 10px;
    }

    #toc-content ol li {
        padding-left: 10px;
    }

    #toc-content .toc-link:hover {
        color: #42b983;
        font-weight: 700;
        text-decoration: underline;
    }

    #toc-content .toc-link::before {
        background-color: transparent;
        max-height: 25px;

        position: absolute;
        right: 23.5vw;
        display: block;
    }

    #toc-content .is-active-link {
        color: #42b983;
    }

    #floating-toc-btn {
        position: fixed;
        right: 15px;
        bottom: 76px;
        padding-top: 15px;
        margin-bottom: 0;
        z-index: 998;
    }

    #floating-toc-btn .btn-floating {
        width: 48px;
        height: 48px;
    }

    #floating-toc-btn .btn-floating i {
        line-height: 48px;
        font-size: 1.4rem;
    }
</style>
<div class="row">
    <div id="main-content" class="col s12 m12 l9">
        <!-- 文章内容详情 -->
<div id="artDetail">
    <div class="card">
        <div class="card-content article-info">
            <div class="row tag-cate">
                <div class="col s7">
                    
                          <div class="article-tag">
                            <span class="chip bg-color">无标签</span>
                          </div>
                    
                </div>
                <div class="col s5 right-align">
                    
                </div>
            </div>

            <div class="post-info">
                
                <div class="post-date info-break-policy">
                    <i class="far fa-calendar-minus fa-fw"></i>发布日期:&nbsp;&nbsp;
                    2021-03-29
                </div>
                

                
                <div class="post-date info-break-policy">
                    <i class="far fa-calendar-check fa-fw"></i>更新日期:&nbsp;&nbsp;
                    2021-03-29
                </div>
                

                

                

                
            </div>
        </div>
        <hr class="clearfix">

        

        

        <div class="card-content article-card-content">
            <div id="articleContent">
                <p>@<a href="MybatisPlus%E7%AC%94%E8%AE%B0">TOC</a></p>
<h1 id="一、简介"><a href="#一、简介" class="headerlink" title="一、简介"></a>一、简介</h1><h2 id="1-MyBatisPlus介绍"><a href="#1-MyBatisPlus介绍" class="headerlink" title="1.MyBatisPlus介绍"></a>1.MyBatisPlus介绍</h2><p>MyBatis-Plus( 简称 MP),是一个 MyBatis 的增强工具包，只做增强不做改变. 为简化开<br>发工作、提高生产率而生，我们的愿景是成为 Mybatis 最好的搭档，就像 魂斗罗 中的 1P、2P，基友搭配，效率翻倍。</p>
<h2 id="2-代码及文档发布地址"><a href="#2-代码及文档发布地址" class="headerlink" title="2.代码及文档发布地址"></a>2.代码及文档发布地址</h2><p>官方地址:<br><a target="_blank" rel="noopener" href="http://mp.baomidou.com/">http://mp.baomidou.com</a><br>代码发布地址:<br>Github: <a target="_blank" rel="noopener" href="https://github.com/baomidou/mybatis-plus">https://github.com/baomidou/mybatis-plus</a><br>Gitee: <a target="_blank" rel="noopener" href="https://gitee.com/baomidou/mybatis-plus">https://gitee.com/baomidou/mybatis-plus</a><br>文档发布地址:<br><a target="_blank" rel="noopener" href="https://mp.baomidou.com/guide/">https://mp.baomidou.com/guide/</a><br>代码地址：<br><a target="_blank" rel="noopener" href="https://github.com/ZKQevin/MybatisPlus">https://github.com/ZKQevin/MybatisPlus</a><br>学习地址：<br><a target="_blank" rel="noopener" href="https://www.bilibili.com/video/BV1Ds411E76Y">https://www.bilibili.com/video/BV1Ds411E76Y</a></p>
<h1 id="二、集成MP"><a href="#二、集成MP" class="headerlink" title="二、集成MP"></a>二、集成MP</h1><h2 id="1-创建数据库表"><a href="#1-创建数据库表" class="headerlink" title="1.创建数据库表"></a>1.创建数据库表</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">-- 创建库 </span><br><span class="line">CREATE DATABASE mp; </span><br><span class="line">-- 使用库 </span><br><span class="line">USE mp; </span><br><span class="line">-- 创建表 </span><br><span class="line">CREATE TABLE tbl_employee( </span><br><span class="line">id INT(11) PRIMARY KEY AUTO_INCREMENT, </span><br><span class="line">last_name VARCHAR(50), </span><br><span class="line">email VARCHAR(50), </span><br><span class="line">gender CHAR(1),</span><br><span class="line"> age int </span><br><span class="line"> ); </span><br><span class="line">INSERT INTO tbl_employee(last_name,email,gender,age) VALUES(&#39;Tom&#39;,&#39;tom@qq.com&#39;,1,22); </span><br><span class="line">INSERT INTO tbl_employee(last_name,email,gender,age) VALUES(&#39;Jerry&#39;,&#39;jerry@qq.com&#39;,0,25); </span><br><span class="line">INSERT INTO tbl_employee(last_name,email,gender,age) VALUES(&#39;Black&#39;,&#39;black@qq.com&#39;,1,30); </span><br><span class="line">INSERT INTO tbl_employee(last_name,email,gender,age) VALUES(&#39;White&#39;,&#39;white@qq.com&#39;,0,35);</span><br></pre></td></tr></table></figure>
<h2 id="2-创建JavaBean实体类"><a href="#2-创建JavaBean实体类" class="headerlink" title="2.创建JavaBean实体类"></a>2.创建JavaBean实体类</h2><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">public <span class="class"><span class="keyword">class</span> <span class="title">Employee</span> </span>&#123;</span><br><span class="line">    private Integer id;</span><br><span class="line">    private <span class="built_in">String</span> lastName;</span><br><span class="line">    private <span class="built_in">String</span> email;</span><br><span class="line">    private Integer gender;</span><br><span class="line">    private Integer age;</span><br><span class="line">    <span class="comment">//省略get和set以及ToString方法。</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="3-添加依赖"><a href="#3-添加依赖" class="headerlink" title="3.添加依赖"></a>3.添加依赖</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><span class="line">&lt;!--单元测试依赖--&gt;</span><br><span class="line">       &lt;dependency&gt;</span><br><span class="line">           &lt;groupId&gt;junit&lt;&#x2F;groupId&gt;</span><br><span class="line">           &lt;artifactId&gt;junit&lt;&#x2F;artifactId&gt;</span><br><span class="line">           &lt;version&gt;4.11&lt;&#x2F;version&gt;</span><br><span class="line">           &lt;scope&gt;test&lt;&#x2F;scope&gt;</span><br><span class="line">       &lt;&#x2F;dependency&gt;</span><br><span class="line">       &lt;!--mp依赖</span><br><span class="line">           MybatisPlus 会自动维护Mybatis以及Mybatis-spring相关的依赖--&gt;</span><br><span class="line">       &lt;dependency&gt;</span><br><span class="line">           &lt;groupId&gt;com.baomidou&lt;&#x2F;groupId&gt;</span><br><span class="line">           &lt;artifactId&gt;mybatis-plus&lt;&#x2F;artifactId&gt;</span><br><span class="line">           &lt;version&gt;3.1.2&lt;&#x2F;version&gt;</span><br><span class="line">       &lt;&#x2F;dependency&gt;</span><br><span class="line">       &lt;!--log4j--&gt;</span><br><span class="line">       &lt;dependency&gt;</span><br><span class="line">           &lt;groupId&gt;log4j&lt;&#x2F;groupId&gt;</span><br><span class="line">           &lt;artifactId&gt;log4j&lt;&#x2F;artifactId&gt;</span><br><span class="line">           &lt;version&gt;1.2.17&lt;&#x2F;version&gt;</span><br><span class="line">       &lt;&#x2F;dependency&gt;</span><br><span class="line">       &lt;!--c3p0--&gt;</span><br><span class="line">       &lt;dependency&gt;</span><br><span class="line">           &lt;groupId&gt;com.mchange&lt;&#x2F;groupId&gt;</span><br><span class="line">           &lt;artifactId&gt;c3p0&lt;&#x2F;artifactId&gt;</span><br><span class="line">           &lt;version&gt;0.9.5.2&lt;&#x2F;version&gt;</span><br><span class="line">       &lt;&#x2F;dependency&gt;</span><br><span class="line">       &lt;!--mysql--&gt;</span><br><span class="line">       &lt;dependency&gt;</span><br><span class="line">           &lt;groupId&gt;mysql&lt;&#x2F;groupId&gt;</span><br><span class="line">           &lt;artifactId&gt;mysql-connector-java&lt;&#x2F;artifactId&gt;</span><br><span class="line">           &lt;version&gt;5.1.38&lt;&#x2F;version&gt;</span><br><span class="line">       &lt;&#x2F;dependency&gt;</span><br><span class="line">       &lt;!--spring--&gt;</span><br><span class="line">       &lt;dependency&gt;</span><br><span class="line">           &lt;groupId&gt;org.springframework&lt;&#x2F;groupId&gt;</span><br><span class="line">           &lt;artifactId&gt;spring-context&lt;&#x2F;artifactId&gt;</span><br><span class="line">           &lt;version&gt;4.3.13.RELEASE&lt;&#x2F;version&gt;</span><br><span class="line">       &lt;&#x2F;dependency&gt;</span><br><span class="line">       &lt;dependency&gt;</span><br><span class="line">           &lt;groupId&gt;org.springframework&lt;&#x2F;groupId&gt;</span><br><span class="line">           &lt;artifactId&gt;spring-orm&lt;&#x2F;artifactId&gt;</span><br><span class="line">           &lt;version&gt;4.3.24.RELEASE&lt;&#x2F;version&gt;</span><br><span class="line">       &lt;&#x2F;dependency&gt;</span><br></pre></td></tr></table></figure>
<h2 id="4-引入数据源配置部分省略"><a href="#4-引入数据源配置部分省略" class="headerlink" title="4.引入数据源配置部分省略"></a>4.引入数据源配置部分省略</h2><h1 id="三、通用CRUD"><a href="#三、通用CRUD" class="headerlink" title="三、通用CRUD"></a>三、通用CRUD</h1><p>1)基于 Mybatis<br>需要编写 EmployeeMapper 接口，并手动编写 CRUD 方法<br>提供 EmployeeMapper.xml 映射文件，并手动编写每个方法对应的 SQL 语句.<br>2)基于 MybatisPlus<br>只需要创建 EmployeeMapper 接口, 并继承 BaseMapper 接口.这就是使用 M<br>(泛型指定的就是当前Mapper接口所操作的实体类)需要完成的所有操作，甚至不需要创建 SQL 映射文件。</p>
<h2 id="1-插入操作"><a href="#1-插入操作" class="headerlink" title="1.插入操作"></a>1.插入操作</h2><p> <strong>①.生成ID策略</strong><br><img src="https://img-blog.csdnimg.cn/20210204142803599.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQxMDQ5Mzcx,size_16,color_FFFFFF,t_70" alt="在这里插入图片描述"><br><strong>②.开启驼峰命名转换以及对应数据库表</strong></p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">@TableName(value = <span class="string">&quot;tbl_employee&quot;</span>)<span class="comment">//对应数据库名</span></span><br><span class="line">public <span class="class"><span class="keyword">class</span> <span class="title">Employee</span> </span>&#123;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * TableId:</span></span><br><span class="line"><span class="comment">     *  value：指定表中的主键列的列明，如果实体属性名与类名一直，可以省略不指定，</span></span><br><span class="line"><span class="comment">     *  type：指定主键策略</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    @TableId(value = <span class="string">&quot;id&quot;</span>,type = IdType.AUTO)</span><br><span class="line">    private Integer id;</span><br><span class="line">    private <span class="built_in">String</span> lastName;</span><br><span class="line">    private <span class="built_in">String</span> email;</span><br><span class="line">    private Integer gender;</span><br><span class="line">    private Integer age;</span><br><span class="line">    @TableField(exist = <span class="literal">false</span>)<span class="comment">//忽略数据库没有的值salary</span></span><br><span class="line">    private Integer salary;</span><br><span class="line">    <span class="comment">//省略get和set方法</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><strong>③.获取插入的ID值</strong><br>在<strong>mybatis</strong>的Mapper映射文件中</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">&lt;insert id&#x3D;&quot;insert&quot; useGeneratedKeys&#x3D;&quot;true&quot; keyProperty&#x3D;&quot;id&quot;&gt;&lt;&#x2F;insert&gt;</span><br></pre></td></tr></table></figure>
<p>在<strong>MybatisPlus</strong>中，直接在Java后台获取到不需要写Mapper</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">Integer key=employee.getId();</span><br></pre></td></tr></table></figure>
<p><strong>④.插入对象出现空值</strong></p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">@Test</span><br><span class="line">public <span class="keyword">void</span> <span class="function"><span class="title">testCommonInsert</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">        <span class="comment">//初始化Employee对象</span></span><br><span class="line">        Employee employee = <span class="keyword">new</span> Employee();</span><br><span class="line">        employee.setLastName(<span class="string">&quot;MP&quot;</span>);</span><br><span class="line">        employee.setEmail(<span class="string">&quot;kevin@qq.com&quot;</span>);</span><br><span class="line">        Integer result = employeeMapper.insert(employee);</span><br><span class="line">        System.out.println(<span class="string">&quot;Result：&quot;</span> + result);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>此时插入结果为：MP,<a href="mailto:&#107;&#101;&#118;&#105;&#x6e;&#x40;&#113;&#x71;&#46;&#99;&#x6f;&#109;">&#107;&#101;&#118;&#105;&#x6e;&#x40;&#113;&#x71;&#46;&#99;&#x6f;&#109;</a>，null，null。<br><strong>结论</strong>：insert方法在插入时，会根据实体类的每个属性进行非空判断，只有非空的属性对应的字段才会出现到SQL语句中</p>
<h2 id="2-更新操作"><a href="#2-更新操作" class="headerlink" title="2.更新操作"></a>2.更新操作</h2><p>通过ID进行更新操作，传入一个对相同会进行自动判断</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">@Test</span><br><span class="line">    public <span class="keyword">void</span> <span class="function"><span class="title">testCommonUpdate</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        Employee employee = <span class="keyword">new</span> Employee();</span><br><span class="line">        employee.setId(<span class="number">6</span>);</span><br><span class="line">        employee.setLastName(<span class="string">&quot;MybatisPlus&quot;</span>);</span><br><span class="line">        employee.setEmail(<span class="string">&quot;MybatisPlus@qq.com&quot;</span>);</span><br><span class="line">        employee.setGender(<span class="number">0</span>);</span><br><span class="line">        <span class="comment">//可以写一个构造方法，new Employee(6,&quot;Name&quot;,&quot;email&quot;,0,20)</span></span><br><span class="line">        <span class="comment">//如果出现非空会MybatisPlus会进行自动判断，不会出现在SQL语句中</span></span><br><span class="line">        employee.setAge(<span class="number">20</span>);</span><br><span class="line">        Integer result = employeeMapper.updateById(employee);</span><br><span class="line">        System.out.println(<span class="string">&quot;result：&quot;</span>+result);</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>
<h2 id="3-查询操作"><a href="#3-查询操作" class="headerlink" title="3.查询操作"></a>3.查询操作</h2><p><strong>①.通过ID查询</strong></p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">@Test</span><br><span class="line">    public <span class="keyword">void</span> <span class="function"><span class="title">testCommonSelect</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        Employee employee = employeeMapper.selectById(<span class="number">3</span>);</span><br><span class="line">        System.out.println(<span class="string">&quot;Employee=&quot;</span>+employee);</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>
<p><strong>②.通过实体查询单条数据</strong></p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">Employee employee = <span class="keyword">new</span> Employee();</span><br><span class="line">employee.setId(<span class="number">4</span>);</span><br><span class="line">employee.setLastName(<span class="string">&quot;DSM&quot;</span>);</span><br><span class="line">Employee result = employeeMapper.selectOne(employee);</span><br><span class="line">System.out.println(result);</span><br></pre></td></tr></table></figure>
<p><strong>③.传入多个ID查询多条记录</strong></p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">List&lt;Integer&gt; idList=<span class="keyword">new</span> ArrayList&lt;&gt;();</span><br><span class="line">idList.add(<span class="number">4</span>);</span><br><span class="line">idList.add(<span class="number">5</span>);</span><br><span class="line">idList.add(<span class="number">6</span>);</span><br><span class="line">List&lt;Employee&gt; employees = employeeMapper.selectBatchIds(idList);</span><br><span class="line">System.out.println(employees);</span><br></pre></td></tr></table></figure>
<p><strong>④.使用HashMap封装对象查询</strong></p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">Map</span>&lt;<span class="built_in">String</span>, <span class="built_in">Object</span>&gt; columnMap=<span class="keyword">new</span> HashMap&lt;&gt;();</span><br><span class="line">      columnMap.put(<span class="string">&quot;last_name&quot;</span>,<span class="string">&quot;Tom&quot;</span>);</span><br><span class="line">      columnMap.put(<span class="string">&quot;gender&quot;</span>,<span class="number">1</span>);</span><br><span class="line">      List&lt;Employee&gt; result = employeeMapper.selectByMap(columnMap);</span><br><span class="line">      System.out.println(result);</span><br></pre></td></tr></table></figure>
<p><strong>⑤.分页查询</strong><br>查询第三页，每一页显示两条数据，MybatisPlus会进行自动分页处理，底层使用的Mybatis的RowBounds方法进行处理，内存进行处理，所以SQL是看不到的。后续会使用MybatisPlus的插件进行物理分页</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">IPage&lt;Employee&gt; result = employeeMapper.selectPage(<span class="keyword">new</span> Page&lt;&gt;(<span class="number">3</span>, <span class="number">2</span>), <span class="literal">null</span>);</span><br><span class="line">        System.out.println(result);</span><br></pre></td></tr></table></figure>
<h2 id="3-删除操作"><a href="#3-删除操作" class="headerlink" title="3.删除操作"></a>3.删除操作</h2><p><strong>①.通过id进行删除</strong></p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">@Test</span><br><span class="line">    public <span class="keyword">void</span> <span class="function"><span class="title">testCommonDelete</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">        Integer result = employeeMapper.deleteById(<span class="number">8</span>);</span><br><span class="line">        System.out.println(<span class="string">&quot;Result：&quot;</span>+result);</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>
<p><strong>②.根据条件删除</strong></p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">Map</span>&lt;<span class="built_in">String</span>, <span class="built_in">Object</span>&gt; columnMap=<span class="keyword">new</span> HashMap&lt;&gt;();</span><br><span class="line">      columnMap.put(<span class="string">&quot;last_name&quot;</span>,<span class="string">&quot;MP&quot;</span>);</span><br><span class="line">      columnMap.put(<span class="string">&quot;email&quot;</span>,<span class="string">&quot;kevin@qq.com&quot;</span>);</span><br><span class="line">      Integer result = employeeMapper.deleteByMap(columnMap);</span><br><span class="line">      System.out.println(<span class="string">&quot;result&quot;</span>+result);</span><br></pre></td></tr></table></figure>
<p><strong>③.批量删除数据</strong></p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">List&lt;Integer&gt; idList = <span class="keyword">new</span> ArrayList&lt;&gt;();</span><br><span class="line">      idList.add(<span class="number">12</span>);</span><br><span class="line">      idList.add(<span class="number">13</span>);</span><br><span class="line">      Integer result = employeeMapper.deleteBatchIds(idList);</span><br><span class="line">      System.out.println(<span class="string">&quot;result&quot;</span>+result);</span><br></pre></td></tr></table></figure>
<h2 id="4-MybatisPlus启动注入SQL原理分析"><a href="#4-MybatisPlus启动注入SQL原理分析" class="headerlink" title="4.MybatisPlus启动注入SQL原理分析"></a>4.MybatisPlus启动注入SQL原理分析</h2><p>问题: xxxMapper 继承了 BaseMapper, BaseMapper 中提供了通用的 CRUD 方法, 方法来源于 BaseMapper, 有方法就必须有 SQL, 因为 MyBatis 最终还是需要通过 SQL 语句操作数据。</p>
<h1 id="四、条件构造器"><a href="#四、条件构造器" class="headerlink" title="四、条件构造器"></a>四、条件构造器</h1><h2 id="1-QueryWrapper（条件查询构造器）"><a href="#1-QueryWrapper（条件查询构造器）" class="headerlink" title="1.QueryWrapper（条件查询构造器）"></a>1.QueryWrapper（条件查询构造器）</h2><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><span class="line">public Employee <span class="function"><span class="title">testQueryWrapperSelect</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">		<span class="comment">//1.我们需要分页查询tbl_employee表中，年龄在16-50之间性别为男性姓名为MP的所有用户</span></span><br><span class="line">        IPage&lt;Employee&gt; employeeIPage = employeeMapper.selectPage(<span class="keyword">new</span> Page&lt;Employee&gt;(<span class="number">1</span>, <span class="number">2</span>),</span><br><span class="line">                <span class="keyword">new</span> QueryWrapper&lt;Employee&gt;()</span><br><span class="line">                        .between(<span class="string">&quot;age&quot;</span>, <span class="number">18</span>, <span class="number">50</span>)</span><br><span class="line">                        .eq(<span class="string">&quot;gender&quot;</span>, <span class="number">1</span>)</span><br><span class="line">                        .eq(<span class="string">&quot;last_name&quot;</span>, <span class="string">&quot;Tom&quot;</span>));</span><br><span class="line">        List&lt;Employee&gt; employees1=employeeIPage.getRecords();</span><br><span class="line">        employees1.forEach((value)-&gt; System.out.println(value));</span><br><span class="line"></span><br><span class="line">        <span class="comment">//2.查询tbl_employee表中，性别为女并且名字中带有“Tom”或者邮箱中带有“a“</span></span><br><span class="line">        List&lt;Employee&gt; employees2 = employeeMapper.selectList(<span class="keyword">new</span> QueryWrapper&lt;Employee&gt;()</span><br><span class="line">                .eq(<span class="string">&quot;gender&quot;</span>, <span class="number">0</span>)</span><br><span class="line">                .like(<span class="string">&quot;last_name&quot;</span>, <span class="string">&quot;Tom&quot;</span>)</span><br><span class="line">                .or()   <span class="comment">//SQL:(gender = ? AND last_name LIKE ? OR email LIKE ? )</span></span><br><span class="line">                <span class="comment">//.orNew()  //SQL:(gender = ? AND last_name LIKE ?) OR (email LIKE ? );貌似新版本已经取消orNew()了</span></span><br><span class="line">                .like(<span class="string">&quot;email&quot;</span>, <span class="string">&quot;a&quot;</span>)</span><br><span class="line">        );</span><br><span class="line">        employees2.forEach(value -&gt; System.out.println(value));</span><br><span class="line"></span><br><span class="line">        <span class="comment">//3.使用last。查询为女的，根据age进行排序（asc/desc），进行分页</span></span><br><span class="line">        List&lt;Employee&gt; employees3 = employeeMapper.selectList(<span class="keyword">new</span> QueryWrapper&lt;Employee&gt;()</span><br><span class="line">                .eq(<span class="string">&quot;gender&quot;</span>, <span class="number">0</span>)</span><br><span class="line">                .orderByDesc(<span class="string">&quot;age&quot;</span>)</span><br><span class="line">                .last(<span class="string">&quot;limit 1,3&quot;</span>));</span><br><span class="line">        employees3.forEach(value -&gt; System.out.println(value));</span><br><span class="line"></span><br><span class="line">        <span class="comment">//4.使用Condition分页查询tbl_employee表中，年龄在16-50之间性别为男性姓名为MP的所有用户（了解Condition即可）</span></span><br><span class="line">        <span class="comment">//employeeMapper.selectPage(new Page&lt;Employee&gt;(1,2),Connection.create().eq(&quot;gender&quot;,1));</span></span><br><span class="line">     	<span class="comment">//5.另一种分页,获取每行存在list集合中，并且每行的字段都放在map集合中。key:value=columnName:value</span></span><br><span class="line">    	Page&lt;<span class="built_in">Map</span>&lt;<span class="built_in">String</span>,<span class="built_in">Object</span>&gt;&gt; page=employeeMapper.selectMapsPage(<span class="keyword">new</span> Page&lt;<span class="built_in">Map</span>&lt;<span class="built_in">String</span>,<span class="built_in">Object</span>&gt;&gt;(<span class="number">1</span>,<span class="number">3</span>),</span><br><span class="line">                                                                <span class="keyword">new</span> QueryWrapper&lt;Employee&gt;().lambda()</span><br><span class="line">                                                                .between(Employee::getAge,<span class="number">15</span>,<span class="number">50</span>)</span><br><span class="line">                                                                .eq(Employee::getGender,<span class="number">1</span>)</span><br><span class="line">                                                                .eq(Employee::getLastName,<span class="string">&quot;MP&quot;</span>));</span><br><span class="line">    	List&lt;<span class="built_in">Map</span>&lt;<span class="built_in">String</span>,<span class="built_in">Object</span>&gt;&gt; emps=page.getRecords();</span><br><span class="line">    	<span class="keyword">for</span>(<span class="built_in">Map</span>&lt;<span class="built_in">String</span>,<span class="built_in">Object</span>&gt; map:emps)&#123;</span><br><span class="line">        	<span class="keyword">for</span>(<span class="built_in">String</span> key:map.keySet())&#123;</span><br><span class="line">            	System.out.print(key+<span class="string">&quot;---&gt;&quot;</span>+map.get(key)+<span class="string">&quot;; &quot;</span>);</span><br><span class="line">        &#125;</span><br><span class="line">        	System.out.println();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="2-UpdateWrapper（修改构造器）"><a href="#2-UpdateWrapper（修改构造器）" class="headerlink" title="2.UpdateWrapper（修改构造器）"></a>2.UpdateWrapper（修改构造器）</h2><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">@Test</span><br><span class="line">    public <span class="keyword">void</span> <span class="function"><span class="title">testUpdateWrapperSelect</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        Employee employee = <span class="keyword">new</span> Employee();</span><br><span class="line">        employee.setLastName(<span class="string">&quot;Jack&quot;</span>);</span><br><span class="line">        employee.setEmail(<span class="string">&quot;Jack@sina.com&quot;</span>);</span><br><span class="line">        employee.setGender(<span class="number">0</span>);</span><br><span class="line">        Integer update = employeeMapper.update(employee, <span class="keyword">new</span> UpdateWrapper&lt;Employee&gt;()</span><br><span class="line">                        .eq(<span class="string">&quot;last_name&quot;</span>,<span class="string">&quot;Tom&quot;</span>)</span><br><span class="line">                        .eq(<span class="string">&quot;age&quot;</span>,<span class="number">44</span>));</span><br><span class="line">        System.out.println(update);</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>
<h2 id="3-UpdateWrapper（删除构造器）"><a href="#3-UpdateWrapper（删除构造器）" class="headerlink" title="3.UpdateWrapper（删除构造器）"></a>3.UpdateWrapper（删除构造器）</h2><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">@Test</span><br><span class="line">    public <span class="keyword">void</span> <span class="function"><span class="title">testDeleteWrapper</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        Integer <span class="keyword">delete</span> = employeeMapper.delete(<span class="keyword">new</span> UpdateWrapper&lt;Employee&gt;()</span><br><span class="line">                .eq(<span class="string">&quot;last_name&quot;</span>, <span class="string">&quot;Tom&quot;</span>)</span><br><span class="line">                .eq(<span class="string">&quot;age&quot;</span>, <span class="number">20</span>));</span><br><span class="line">        System.out.println(<span class="keyword">delete</span>);</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>
<h2 id="4-小结"><a href="#4-小结" class="headerlink" title="4.小结"></a>4.小结</h2><p>本次教学只是常用的罗列出来，还有很多构造器方法在文档之中，需要自己学<br>MP：多个Wrapper    Condition 条件构造器<br>Mybatis MBG：xxxExample→Criteria：QBC(Query By Criteria) Hibernate、通用Mapper</p>
<h1 id="五、ActiveReccord（活动记录）"><a href="#五、ActiveReccord（活动记录）" class="headerlink" title="五、ActiveReccord（活动记录）"></a>五、ActiveReccord（活动记录）</h1><p><strong>1）概述</strong><br>Active Record(活动记录)，是一种领域模型模式，特点是一个模型类对应关系型数据库中的一个表，而模型类的一个实例对应表中的一行记录。<br>ActiveRecord 一直广受动态语言（ PHP 、 Ruby 等）的喜爱，而 Java 作为准静态语言，对于 ActiveRecord 往往只能感叹其优雅，所以 MP 也在 AR 道路上进行了一定的探索。</p>
<h2 id="1-使用AR：继承Model-lt-实体类-gt"><a href="#1-使用AR：继承Model-lt-实体类-gt" class="headerlink" title="1.使用AR：继承Model&lt;实体类&gt;"></a>1.使用AR：继承Model&lt;实体类&gt;</h2><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">public <span class="class"><span class="keyword">class</span> <span class="title">Employee</span> <span class="keyword">extends</span> <span class="title">Model</span>&lt;<span class="title">Employee</span>&gt; </span>&#123;</span><br><span class="line">	<span class="comment">//省略getset方法以及实体类属性</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="2-AR插入操作"><a href="#2-AR插入操作" class="headerlink" title="2.AR插入操作"></a>2.AR插入操作</h2><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">@Test</span><br><span class="line">    public <span class="keyword">void</span> <span class="function"><span class="title">testARInsert</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        Employee employee = <span class="keyword">new</span> Employee();</span><br><span class="line">        employee.setLastName(<span class="string">&quot;Jack&quot;</span>);</span><br><span class="line">        employee.setEmail(<span class="string">&quot;Jack@qq.com&quot;</span>);</span><br><span class="line">        employee.setGender(<span class="number">1</span>);</span><br><span class="line">        employee.setAge(<span class="number">20</span>);</span><br><span class="line">        boolean insert = employee.insert();</span><br><span class="line">        System.out.println(<span class="string">&quot;Result=&quot;</span>+insert);</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>
<h2 id="3-AR修改操作"><a href="#3-AR修改操作" class="headerlink" title="3.AR修改操作"></a>3.AR修改操作</h2><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">@Test</span><br><span class="line">    public <span class="keyword">void</span> <span class="function"><span class="title">testARUpdate</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        Employee employee = <span class="keyword">new</span> Employee(<span class="number">16</span>,<span class="string">&quot;JKL&quot;</span>,<span class="string">&quot;JKL@qq.com&quot;</span>,<span class="number">0</span>,<span class="number">15</span>);</span><br><span class="line">        System.out.println(employee.updateById());</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>
<h2 id="4-AR删除操作"><a href="#4-AR删除操作" class="headerlink" title="4.AR删除操作"></a>4.AR删除操作</h2><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">@Test</span><br><span class="line">    public <span class="keyword">void</span> <span class="function"><span class="title">testARDelete</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        Employee employee = <span class="keyword">new</span> Employee();</span><br><span class="line">        <span class="comment">//通过id删除数据</span></span><br><span class="line">        System.out.println(<span class="string">&quot;通过ID删除结果=&quot;</span>+employee.deleteById(<span class="number">4</span>));</span><br><span class="line">    &#125;</span><br><span class="line">    	<span class="comment">//根据条件删除数据(在逻辑上删除不存在的数据也是返回True)</span></span><br><span class="line">        System.out.println(<span class="string">&quot;通过条件删除结果=&quot;</span>+employee.delete(<span class="keyword">new</span> QueryWrapper&lt;Employee&gt;().eq(<span class="string">&quot;last_name&quot;</span>,<span class="string">&quot;jack&quot;</span>)));</span><br></pre></td></tr></table></figure>
<h2 id="5-AR查询操作"><a href="#5-AR查询操作" class="headerlink" title="5.AR查询操作"></a>5.AR查询操作</h2><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">@Test</span><br><span class="line">    public <span class="keyword">void</span> <span class="function"><span class="title">testARSelect</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        Employee employee = <span class="keyword">new</span> Employee();</span><br><span class="line">        <span class="comment">//通过id查询</span></span><br><span class="line">        System.out.println(employee.selectById(<span class="number">16</span>));</span><br><span class="line">        <span class="comment">//查询所有操作</span></span><br><span class="line">        System.out.println(employee.selectAll());</span><br><span class="line">        <span class="comment">//使用Wrapper以及AR进行模糊查询</span></span><br><span class="line">        employee.selectList(<span class="keyword">new</span> QueryWrapper&lt;Employee&gt;().like(<span class="string">&quot;last_name&quot;</span>,<span class="string">&quot;J&quot;</span>)).forEach(value -&gt; &#123;</span><br><span class="line">            System.out.println(value);</span><br><span class="line">        &#125;);</span><br><span class="line">        </span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>
<h2 id="6-AR分页复杂操作"><a href="#6-AR分页复杂操作" class="headerlink" title="6.AR分页复杂操作"></a>6.AR分页复杂操作</h2><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">@Test</span><br><span class="line">    public <span class="keyword">void</span> <span class="function"><span class="title">testARPage</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        Employee employee = <span class="keyword">new</span> Employee();</span><br><span class="line">        <span class="comment">//selectpage方法将返回对象封装到一个Page对象中，如果要得到需要使用getRecord()方法。</span></span><br><span class="line">        IPage&lt;Employee&gt; employeeIPage = employee.selectPage(<span class="keyword">new</span> Page&lt;&gt;(<span class="number">1</span>, <span class="number">1</span>), <span class="keyword">new</span> QueryWrapper&lt;Employee&gt;().like(<span class="string">&quot;last_name&quot;</span>, <span class="string">&quot;T&quot;</span>));</span><br><span class="line">        employeeIPage.getRecords().forEach(value -&gt; &#123;</span><br><span class="line">            System.out.println(value);</span><br><span class="line">        &#125;);</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>
<h2 id="7-小结"><a href="#7-小结" class="headerlink" title="7.小结"></a>7.小结</h2><p>AR 模式提供了一种更加便捷的方式实现 CRUD 操作，其本质还是调用的 Mybatis 对应的方法，类似于<strong>语法糖</strong>。指计算机语言中添加的某种语法，这种语法对原本语言的功能并没有影响。可以更方便开发者使用，可以避免出错的机会，让程序可读性更好。</p>
<h1 id="六、代码生成器"><a href="#六、代码生成器" class="headerlink" title="六、代码生成器"></a>六、代码生成器</h1><p><strong>1）简介</strong><br>MP 提供了大量的自定义设置，生成的代码完全能够满足各类型的需求。AutoGenerator 是 MyBatis-Plus 的代码生成器，通过 AutoGenerator 可以快速生成 Entity、Mapper、Mapper XML、Service、Controller 等各个模块的代码，极大的提升了开发效率。<br><strong>2）MP 的代码生成器 和 Mybatis MBG代码生成器的对比</strong><br>MP 的代码生成器都是基于 java 代码来生成。MBG 基于 xml 文件代码生成<br>MyBatis 的代码生成器可生成: 实体类、Mapper 接口、Mapper 映射文件</p>
<p>MP的代码生成器可生成: 实体类(可以选择是否支持 AR)、Mapper 接口、Mapper 映射文件、 Service 层、Controller层。IDEA的EasyCode也是可以进行生成。<br><strong>3）表及字段命名策略选择</strong><br>在MybatisPlus中，我们建议<strong>数据库表名</strong> 和 <strong>表字段名</strong>采用<strong>驼峰</strong>命名方式， 如果采用下划线命名方式，请开启全局下划线开关（版本2.3默认开启），如果表名字段名命名方式不一致请注解指定，我们建议最好保持一致（<strong>约定大于配置</strong>）</p>
<p>这么做的原因是为了避免在对应实体类时产生的性能损耗，这样字段不用做映射就能直接和实体类对应。当然如果项目里不用考虑这点性能损耗，那么你采用下滑线也是没问题的，只需要在生成代码时配置<strong>dbColumnUnderline</strong>(map-underscore-to-camel-case)属性就可以。</p>
<h2 id="1-使用添加依赖"><a href="#1-使用添加依赖" class="headerlink" title="1.使用添加依赖"></a>1.使用添加依赖</h2><p>添加 模板引擎 依赖和日志依赖，MyBatis-Plus 支持 <strong>Velocity</strong>（默认）、<strong>Freemarker</strong>、<strong>Beetl</strong>，用户可以选择自己熟悉的模板引擎，如果都不满足要求，可以采用自定义模板引擎。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">&lt;!--Apach Velocity模版依赖--&gt;</span><br><span class="line">        &lt;dependency&gt;</span><br><span class="line">            &lt;groupId&gt;org.apache.velocity&lt;&#x2F;groupId&gt;</span><br><span class="line">            &lt;artifactId&gt;velocity&lt;&#x2F;artifactId&gt;</span><br><span class="line">            &lt;version&gt;1.7&lt;&#x2F;version&gt;</span><br><span class="line">        &lt;&#x2F;dependency&gt;</span><br><span class="line">&lt;!--sfl4j日志依赖--&gt;</span><br><span class="line">        &lt;dependency&gt;</span><br><span class="line">            &lt;groupId&gt;org.slf4j&lt;&#x2F;groupId&gt;</span><br><span class="line">            &lt;artifactId&gt;slf4j-api&lt;&#x2F;artifactId&gt;</span><br><span class="line">            &lt;version&gt;1.7.7&lt;&#x2F;version&gt;</span><br><span class="line">        &lt;&#x2F;dependency&gt;</span><br><span class="line">        &lt;dependency&gt;</span><br><span class="line">            &lt;groupId&gt;org.slf4j&lt;&#x2F;groupId&gt;</span><br><span class="line">            &lt;artifactId&gt;slf4j-log4j12&lt;&#x2F;artifactId&gt;</span><br><span class="line">            &lt;version&gt;1.7.7&lt;&#x2F;version&gt;</span><br><span class="line">        &lt;&#x2F;dependency&gt;</span><br></pre></td></tr></table></figure>
<h2 id="2-生成器代码"><a href="#2-生成器代码" class="headerlink" title="2.生成器代码"></a>2.生成器代码</h2><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br></pre></td><td class="code"><pre><span class="line">@Test</span><br><span class="line">    public <span class="keyword">void</span> <span class="function"><span class="title">testGenerator</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">        <span class="comment">//1.全局策略配置</span></span><br><span class="line">        GlobalConfig config = <span class="keyword">new</span> GlobalConfig();</span><br><span class="line">        config.setActiveRecord(<span class="literal">true</span>)<span class="comment">//是否支持AR模式</span></span><br><span class="line">                .setAuthor(<span class="string">&quot;kevin&quot;</span>)<span class="comment">//作者</span></span><br><span class="line">                .setOutputDir(<span class="string">&quot;D:\\Spring\\Spring-couse\\MyBatisPlus03\\src\\main\\java&quot;</span>)<span class="comment">//输出路径</span></span><br><span class="line">                .setFileOverride(<span class="literal">true</span>)<span class="comment">//文件覆盖</span></span><br><span class="line">                .setIdType(IdType.AUTO)<span class="comment">//主键策略</span></span><br><span class="line">                .setServiceName(<span class="string">&quot;%sService&quot;</span>)<span class="comment">//设置生成service接口名字的首字母是否为I（默认会生成I开头的IStudentService）</span></span><br><span class="line">                .setBaseResultMap(<span class="literal">true</span>)<span class="comment">//自动SQL映射文件，生成基本的ResultMap</span></span><br><span class="line">                .setBaseColumnList(<span class="literal">true</span>);<span class="comment">//生成基本的SQL片段</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">//2.数据源配置</span></span><br><span class="line">        DataSourceConfig dataSourceConfig = <span class="keyword">new</span> DataSourceConfig();</span><br><span class="line">        dataSourceConfig.setDbType(DbType.MYSQL)<span class="comment">//设置数据库类型</span></span><br><span class="line">                .setDriverName(<span class="string">&quot;com.mysql.jdbc.Driver&quot;</span>)<span class="comment">//数据库驱动名</span></span><br><span class="line">                .setUrl(<span class="string">&quot;jdbc:mysql://localhost:3306/mp?useUnicode=true&amp;characterEncoding=UTF-8&quot;</span>)<span class="comment">//数据库地址</span></span><br><span class="line">                .setUsername(<span class="string">&quot;root&quot;</span>)<span class="comment">//数据库名字</span></span><br><span class="line">                .setPassword(<span class="string">&quot;123&quot;</span>);<span class="comment">//数据库密码</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">//3.策略配置</span></span><br><span class="line">        StrategyConfig strategy = <span class="keyword">new</span> StrategyConfig();</span><br><span class="line">        strategy.setCapitalMode(<span class="literal">true</span>)<span class="comment">//全局大写命名</span></span><br><span class="line">                .setDbColumnUnderline(<span class="literal">true</span>)<span class="comment">//指定表明 字段名是否使用下划线</span></span><br><span class="line">                .setNaming(NamingStrategy.underline_to_camel)<span class="comment">//数据库表映射到实体的命名策略</span></span><br><span class="line">                .setTablePrefix(<span class="string">&quot;tbl_&quot;</span>)<span class="comment">//前置命名</span></span><br><span class="line">                .setInclude(<span class="string">&quot;tbl_employee&quot;</span>);<span class="comment">//生成的表</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">//4.包名策略</span></span><br><span class="line">        PackageConfig packageConfig = <span class="keyword">new</span> PackageConfig();</span><br><span class="line">        packageConfig.setParent(<span class="string">&quot;org.example&quot;</span>)<span class="comment">//所放置的包</span></span><br><span class="line">                .setMapper(<span class="string">&quot;mapper&quot;</span>)<span class="comment">//Mapper包</span></span><br><span class="line">                .setService(<span class="string">&quot;service&quot;</span>)<span class="comment">//服务层包</span></span><br><span class="line">                .setController(<span class="string">&quot;controller&quot;</span>)<span class="comment">//控制层</span></span><br><span class="line">                .setEntity(<span class="string">&quot;beans&quot;</span>)<span class="comment">//实体类</span></span><br><span class="line">                .setXml(<span class="string">&quot;mapper&quot;</span>);<span class="comment">//映射文件</span></span><br><span class="line">        <span class="comment">//5.整合配置</span></span><br><span class="line">        AutoGenerator autoGenerator = <span class="keyword">new</span> AutoGenerator();</span><br><span class="line">        autoGenerator.setGlobalConfig(config)</span><br><span class="line">                .setDataSource(dataSourceConfig)</span><br><span class="line">                .setStrategy(strategy)</span><br><span class="line">                .setPackageInfo(packageConfig);</span><br><span class="line">        <span class="comment">//6.执行</span></span><br><span class="line">        autoGenerator.execute();</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>
<h2 id="3-关于服务层自动注入"><a href="#3-关于服务层自动注入" class="headerlink" title="3.关于服务层自动注入"></a>3.关于服务层自动注入</h2><p>Service层的接口自动继承了了<strong>IService</strong>接口，里面有一些常用操作的接口<br>Service层的实现类不仅实现了Service层的接口，并且还继承了ServiceImpl类，该类也实现了IService接口。所以我们的serviceimpl不需要写一些简单具体操作的实现。并且也不需要自动注入mapper，在ServiceImpl中已经帮我们注入了。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//服务层接口</span></span><br><span class="line">public interface EmployeeService <span class="keyword">extends</span> IService&lt;Employee&gt; &#123;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//服务层实现类</span></span><br><span class="line">@Service</span><br><span class="line">public <span class="class"><span class="keyword">class</span> <span class="title">EmployeeServiceImpl</span> <span class="keyword">extends</span> <span class="title">ServiceImpl</span>&lt;<span class="title">EmployeeMapper</span>, <span class="title">Employee</span>&gt; <span class="title">implements</span> <span class="title">EmployeeService</span> </span>&#123;</span><br><span class="line">    <span class="comment">//不用再进行Mapper的注入</span></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * EmployeeServiceImpl  集成了ServiceImpl</span></span><br><span class="line"><span class="comment">     * 1.在ServiceImpl中已经完成Mapper对象的注入，直接在EmployeeServiceImpl中进行使用</span></span><br><span class="line"><span class="comment">     * 2.在ServiceImpl中野帮我们提供了常用的CRUD方法，基本的一些CRUD方法在Service中不需要我们自己定义</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//被继承的接口</span></span><br><span class="line">public <span class="class"><span class="keyword">class</span> <span class="title">ServiceImpl</span>&lt;<span class="title">M</span> <span class="keyword">extends</span> <span class="title">BaseMapper</span>&lt;<span class="title">T</span>&gt;, <span class="title">T</span>&gt; <span class="title">implements</span> <span class="title">IService</span>&lt;<span class="title">T</span>&gt; </span>&#123;</span><br><span class="line">    @Autowired</span><br><span class="line">    protected M baseMapper;<span class="comment">//已自动注入Mapper</span></span><br><span class="line">    <span class="comment">//各种方法省略</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h1 id="七、插件拓展"><a href="#七、插件拓展" class="headerlink" title="七、插件拓展"></a>七、插件拓展</h1><h2 id="1-Mybatis插件机制简介"><a href="#1-Mybatis插件机制简介" class="headerlink" title="1.Mybatis插件机制简介"></a>1.Mybatis插件机制简介</h2><p><strong>1）插件机制</strong><br>Mybatis通过插件(Interkeptor)可以做到拦截四太对象相关方法的执行,根据需求，完成相关数据的动态改变。<br>Executor<br>StatementHandler<br>ParameterHandler<br>ResultSetHandler<br><strong>2）插件原理</strong><br>四太对象的每个对象在创建时，都会执行interceptorChain.pluginAll()，会经过每个插件对象的plugin()方法，目的是为当前的四太对象创建代理。代理对象就可以拦截到四太对象相关方法的执行，因为要执行四太对象的方法需要经过代理。<br><strong>3）注册插件</strong>：在Spring配置文件下sqlSessionFactory的bean标签下</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">&lt;!--注册插件--&gt;</span><br><span class="line">        &lt;property name&#x3D;&quot;plugins&quot;&gt;</span><br><span class="line">            &lt;list&gt;</span><br><span class="line">            	&lt;!--注册分页插件--&gt;</span><br><span class="line">                &lt;bean class&#x3D;&quot;com.baomidou.mybatisplus.plugins.PaginationInterceptor&quot;&gt;&lt;&#x2F;bean&gt;</span><br><span class="line">                &lt;!--注册执行分析插件--&gt;</span><br><span class="line">                &lt;bean class&#x3D;&quot;com.baomidou.mybatisplus.plugins.SqlExplainInterceptor&quot;&gt;</span><br><span class="line">                    &lt;!--停止全表操作--&gt;</span><br><span class="line">                    &lt;property name&#x3D;&quot;stopProceed&quot; value&#x3D;&quot;true&quot;&#x2F;&gt;</span><br><span class="line">                &lt;&#x2F;bean&gt;</span><br><span class="line">                &lt;!--注册性能分析插件--&gt;</span><br><span class="line">                &lt;bean class&#x3D;&quot;com.baomidou.mybatisplus.plugins.PerformanceInterceptor&quot;&gt;</span><br><span class="line">                    &lt;property name&#x3D;&quot;format&quot; value&#x3D;&quot;true&quot;&#x2F;&gt; &lt;!--是否进行格式化--&gt;</span><br><span class="line">                    &lt;property name&#x3D;&quot;maxTime&quot; value&#x3D;&quot;5&quot;&#x2F;&gt; &lt;!--最长执行时间，毫秒级--&gt;</span><br><span class="line">                &lt;&#x2F;bean&gt;</span><br><span class="line">                &lt;!--注册乐观锁插件--&gt;</span><br><span class="line">                &lt;bean class&#x3D;&quot;com.baomidou.mybatisplus.plugins.OptimisticLockerInterceptor&quot;&gt;&lt;&#x2F;bean&gt;</span><br><span class="line">            &lt;&#x2F;list&gt;</span><br><span class="line">        &lt;&#x2F;property&gt;</span><br></pre></td></tr></table></figure>
<p>还可以在Mybatis配置文件下configuration标签下</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">&lt;plugins&gt;</span><br><span class="line">        &lt;plugin interceptor&#x3D;&quot;com.baomidou.mybatisplus.plugins.PaginationInterceptor&quot; &#x2F;&gt;</span><br><span class="line">        …………</span><br><span class="line">&lt;&#x2F;plugins&gt;</span><br></pre></td></tr></table></figure>
<h2 id="2-分页插件"><a href="#2-分页插件" class="headerlink" title="2.分页插件"></a>2.分页插件</h2><p> 测试分页插件</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">@Test</span><br><span class="line">    public <span class="keyword">void</span> <span class="function"><span class="title">testPage</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    	Page&lt;Employee&gt; page = <span class="keyword">new</span> Page&lt;&gt;(<span class="number">1</span>, <span class="number">3</span>);</span><br><span class="line">    	List&lt;Employee&gt; employees = employeeMapper.selectPage(page, <span class="literal">null</span>);</span><br><span class="line">    	employees.forEach(value -&gt; System.out.println(value));</span><br><span class="line">    	System.out.println(<span class="string">&quot;总条数：&quot;</span>+page.getTotal());</span><br><span class="line">        System.out.println(<span class="string">&quot;当前页码：&quot;</span>+page.getCurrent());</span><br><span class="line">    	<span class="comment">//将查询的结果封装page对象中</span></span><br><span class="line">        page.setRecords(employees);</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>
<p>这样就会看到在SQL中出现Limit限制语句是真正的进行了分页操作。我们还可以对查询到的数据进行封装为一个Page对象</p>
<h2 id="3-执行分析插件"><a href="#3-执行分析插件" class="headerlink" title="3.执行分析插件"></a>3.执行分析插件</h2><p>注册插件之后如果使用的对全表的操作，程序会自动摧毁此次请求。在测试阶段使用，不建议在生产阶段使用</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">@Test</span><br><span class="line">    public <span class="keyword">void</span> <span class="function"><span class="title">testSQLExplain</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        employeeMapper.delete(<span class="literal">null</span>);<span class="comment">//全表删除操作</span></span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>
<h2 id="4-性能分析插件"><a href="#4-性能分析插件" class="headerlink" title="4.性能分析插件"></a>4.性能分析插件</h2><p>注册插件之后如果所执行的SQL超过执行时间之后那么此条SQL抛出异常。在测试阶段使用，不建议在生产阶段使用</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">@Test</span><br><span class="line">    public <span class="keyword">void</span> <span class="function"><span class="title">testPerformance</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        Employee employee = <span class="keyword">new</span> Employee(<span class="string">&quot;marry&quot;</span>,<span class="string">&quot;marry@qq.com&quot;</span>,<span class="string">&quot;0&quot;</span>,<span class="number">22</span>);</span><br><span class="line">        employeeMapper.insert(employee);</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>
<h2 id="5-乐观锁插件"><a href="#5-乐观锁插件" class="headerlink" title="5.乐观锁插件"></a>5.乐观锁插件</h2><p><strong>1）实现原理</strong>：取出记录的时候获取当前Version版本号更新的时候带上Version，执行更新时，set version=youVersion+1 where versior=yourVersion。如果Version不对，就更新失败。必须使用@Version注解<br><strong>2）使用方法</strong>：在Spring配置文件ApplicationContext.xml配置文件中注册插件。在实体类和数据库添加字段Version。<br>在实体类上添加@Version注解</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">@Version</span><br><span class="line">private Integer version;</span><br><span class="line"><span class="comment">//省略Get和Set方法</span></span><br></pre></td></tr></table></figure>
<p>测试方法，如果此时Version不与数据库中一致并不会报错只是不会更新。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">@Test</span><br><span class="line">    public <span class="keyword">void</span> <span class="function"><span class="title">testOptimisticLocker</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        Employee employee = <span class="keyword">new</span> Employee();<span class="comment">//更新操作</span></span><br><span class="line">        employee.setId(<span class="number">21</span>);</span><br><span class="line">        employee.setLastName(<span class="string">&quot;Tom&quot;</span>);</span><br><span class="line">        employee.setVersion(<span class="number">2</span>);</span><br><span class="line">        employeeMapper.updateById(employee);</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>
<h1 id="八、自定义全局操作"><a href="#八、自定义全局操作" class="headerlink" title="八、自定义全局操作"></a>八、自定义全局操作</h1><p><strong>简介</strong><br>根据 MybatisPlus的 AutoSgllniector,可以自定义各种你想要的sal ,注入到全局中，相当于自定义Mxbatisplus自动注入的方法。</p>
<p>之前需要在xml中进行配置的SQL语句,现在通过扩展AutoSallnjector在加载mybatis,环境时就注入。</p>
<h2 id="1-AutoSqlInjector注入自定义SQL"><a href="#1-AutoSqlInjector注入自定义SQL" class="headerlink" title="1.AutoSqlInjector注入自定义SQL"></a>1.AutoSqlInjector注入自定义SQL</h2><p>1).Mapper中定义业务方法</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">public interface EmployeeMapper <span class="keyword">extends</span> BaseMapper&lt;Employee&gt;&#123;</span><br><span class="line">    Integer deleteByName();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>2).定义MySqlInjector（新建一个injector包，MySqlInjector类）</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">public <span class="class"><span class="keyword">class</span> <span class="title">MySqlInjector</span> <span class="keyword">extends</span> <span class="title">AutoSqlInjector</span> </span>&#123;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 扩展Inject方法，完成自定义全局操作</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    @Override</span><br><span class="line">    public <span class="keyword">void</span> <span class="function"><span class="title">inject</span>(<span class="params">Configuration configuration, MapperBuilderAssistant builderAssistant, Class&lt;?&gt; mapperClass, Class&lt;?&gt; modelClass, TableInfo table</span>)</span> &#123;</span><br><span class="line">        <span class="comment">//将EmployeeMapper中定义的方法，处理成对应的MappedStatement对象，加入到configuration对象中。</span></span><br><span class="line">        <span class="comment">//注入的SQL语句</span></span><br><span class="line">        <span class="built_in">String</span> sql = <span class="string">&quot;delete from &quot;</span> + table.getTableName() + <span class="string">&quot; where name=JKL&quot;</span>;</span><br><span class="line">        <span class="comment">//注入的方法名    一定要与EmployeeMapper接口中的方法名一致</span></span><br><span class="line">        <span class="built_in">String</span> method = <span class="string">&quot;deleteByName&quot;</span>;</span><br><span class="line">        <span class="comment">//构造SqlSource对象</span></span><br><span class="line">        SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);</span><br><span class="line">        <span class="comment">//构造一个删除的MappedStatement</span></span><br><span class="line">        <span class="built_in">this</span>.addDeleteMappedStatement(mapperClass, method, sqlSource);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>3).自定义的MySqlInjector配置到ApplicationContext.xml全局策略中</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">&lt;!--定义MybatisPlus的全局策略配置，在版本2.3以后就不用管了默认把数据库下划线变成驼峰就可以插入和主键策略问题--&gt;</span><br><span class="line">    &lt;bean id&#x3D;&quot;globalConfiguration&quot; class&#x3D;&quot;com.baomidou.mybatisplus.entity.GlobalConfiguration&quot;&gt;</span><br><span class="line">        &lt;!-- 在2.3版本以后，dbColumnUnderline 默认值就是true --&gt;</span><br><span class="line">        &lt;property name&#x3D;&quot;dbColumnUnderline&quot; value&#x3D;&quot;true&quot;&gt;&lt;&#x2F;property&gt;</span><br><span class="line">        &lt;!--注入自定义全局操作--&gt;</span><br><span class="line">        &lt;property name&#x3D;&quot;sqlInjector&quot; ref&#x3D;&quot;mySqlInjector&quot;&gt;&lt;&#x2F;property&gt;</span><br><span class="line">    &lt;&#x2F;bean&gt;</span><br><span class="line">    &lt;!--定义自定义注入器--&gt;</span><br><span class="line">    &lt;bean id&#x3D;&quot;mySqlInjector&quot; class&#x3D;&quot;org.example.injector.MySqlInjector&quot;&gt;&lt;&#x2F;bean&gt;</span><br></pre></td></tr></table></figure>
<p>4).定义测试类</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">@Test</span><br><span class="line">    public <span class="keyword">void</span> <span class="function"><span class="title">testMySqlInjector</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        Integer i = employeeMapper.deleteByName();</span><br><span class="line">        System.out.println(<span class="string">&quot;Result=&quot;</span>+i);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="2-自定义注入器引用之逻辑删除"><a href="#2-自定义注入器引用之逻辑删除" class="headerlink" title="2.自定义注入器引用之逻辑删除"></a>2.自定义注入器引用之逻辑删除</h2><p><strong>假删除、逻辑删除</strong>：并不会真正的从数据库中讲数据删除掉，而是将当前被删除的这条数据中的一个逻辑删除字段置为删除操作。<strong>举例</strong>：tbl_user  logic_flag=1（可用），logic_flag=-1（不可用）。<br>1)新增数据表</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">CREATE TABLE tbl_user( </span><br><span class="line">id INT(11) PRIMARY KEY AUTO_INCREMENT, </span><br><span class="line">NAME VARCHAR(50),</span><br><span class="line">logic_flag INT(11)</span><br><span class="line"> ); </span><br></pre></td></tr></table></figure>
<p>实体类</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">public <span class="class"><span class="keyword">class</span> <span class="title">User</span> </span>&#123;</span><br><span class="line">    private Integer id;</span><br><span class="line">    private <span class="built_in">String</span> name;</span><br><span class="line">    @TableLogic(<span class="comment">/*value = &quot;1&quot;,delval = &quot;-1&quot;*/</span>) <span class="comment">//逻辑删除字段属性，可以使用注解属性</span></span><br><span class="line">    private Integer logicFlag;</span><br><span class="line">    <span class="comment">//省略get和set方法</span></span><br><span class="line">&#125;    </span><br></pre></td></tr></table></figure>
<p>2)在Spring配置文件ApplicationContext.xml进行配置</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">&lt;!--定义MybatisPlus的全局策略配置，在版本2.3以后就不用管了默认把数据库下划线变成驼峰就可以插入和主键策略问题--&gt;</span><br><span class="line">    &lt;bean id&#x3D;&quot;globalConfiguration&quot; class&#x3D;&quot;com.baomidou.mybatisplus.entity.GlobalConfiguration&quot;&gt;</span><br><span class="line">        &lt;!--注入逻辑删除--&gt;</span><br><span class="line">        &lt;property name&#x3D;&quot;sqlInjector&quot; ref&#x3D;&quot;logicSqlInjector&quot;&gt;&lt;&#x2F;property&gt;</span><br><span class="line">        &lt;!--注入逻辑删除全局value配置，也可以在实体类上面进行配置--&gt;</span><br><span class="line">        &lt;property name&#x3D;&quot;logicDeleteValue&quot; value&#x3D;&quot;-1&quot;&#x2F;&gt;</span><br><span class="line">        &lt;property name&#x3D;&quot;logicNotDeleteValue&quot; value&#x3D;&quot;1&quot;&#x2F;&gt;</span><br><span class="line">    &lt;&#x2F;bean&gt;</span><br><span class="line">&lt;!--逻辑删除--&gt;</span><br><span class="line">    &lt;bean id&#x3D;&quot;logicSqlInjector&quot; class&#x3D;&quot;com.baomidou.mybatisplus.mapper.LogicSqlInjector&quot;&#x2F;&gt;</span><br></pre></td></tr></table></figure>
<p>3)测试类</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">@Test</span><br><span class="line">    public <span class="keyword">void</span> <span class="function"><span class="title">testLogicDelete</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">        System.out.println(userMapper.deleteById(<span class="number">1</span>));</span><br><span class="line">        System.out.println(userMapper.selectById(<span class="number">1</span>));</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>
<p>所执行的SQL：UPDATE tbl_user SET logic_flag=-1 WHERE id=? </p>
<h1 id="九、公共字段自动填充"><a href="#九、公共字段自动填充" class="headerlink" title="九、公共字段自动填充"></a>九、公共字段自动填充</h1><h2 id="1-元数据处理器接口"><a href="#1-元数据处理器接口" class="headerlink" title="1.元数据处理器接口"></a>1.元数据处理器接口</h2><p>com.baomidou.mybatisplus.mapper.MetaObjectHandler<br>inserFill(MetaObject metaObject)<br>updateFill(MetaObject metaObject)<br><strong>metaobject:元对象</strong>，是Mybatis.提供的一个用于更加方便，更加优雅的访问对象的属性,给对象的属性设置值的一个对象.还会用于包装对象．支持对object 、Map、Collection等对象进行包装。本质上metaObjet获取对象的属性值或者是给对象的属性设置值，最终是要通过<strong>Reflector</strong>获取到属性的对应方法的<strong>Invoker</strong>，最终invoke。</p>
<h2 id="2-开发步骤"><a href="#2-开发步骤" class="headerlink" title="2.开发步骤"></a>2.开发步骤</h2><p>1)创建包MyMetaObjectHandler并创建类MyMetaObjectHandler继承MetaObjectHandler。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">public <span class="class"><span class="keyword">class</span> <span class="title">MyMetaObjectHandler</span> <span class="keyword">extends</span> <span class="title">MetaObjectHandler</span> </span>&#123;</span><br><span class="line">    @Override</span><br><span class="line">    public <span class="keyword">void</span> <span class="function"><span class="title">insertFill</span>(<span class="params">MetaObject metaObject</span>)</span> &#123;</span><br><span class="line">        <span class="comment">//获取到需要被填充的字段的值</span></span><br><span class="line">        <span class="built_in">Object</span> fieldValue = getFieldValByName(<span class="string">&quot;name&quot;</span>, metaObject);</span><br><span class="line">        <span class="keyword">if</span> (fieldValue == <span class="literal">null</span>) &#123;</span><br><span class="line">            System.out.println(<span class="string">&quot;******************满足填充条件******************&quot;</span>);</span><br><span class="line">            setFieldValByName(<span class="string">&quot;name&quot;</span>, <span class="string">&quot;Kevin&quot;</span>, metaObject);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    @Override</span><br><span class="line">    public <span class="keyword">void</span> <span class="function"><span class="title">updateFill</span>(<span class="params">MetaObject metaObject</span>)</span> &#123;</span><br><span class="line">        <span class="comment">//获取到需要被填充的字段的值</span></span><br><span class="line">        <span class="built_in">Object</span> fieldValue = getFieldValByName(<span class="string">&quot;name&quot;</span>, metaObject);</span><br><span class="line">        <span class="keyword">if</span> (fieldValue == <span class="literal">null</span>) &#123;</span><br><span class="line">            System.out.println(<span class="string">&quot;******************满足填充条件******************&quot;</span>);</span><br><span class="line">            setFieldValByName(<span class="string">&quot;name&quot;</span>, <span class="string">&quot;Jack&quot;</span>, metaObject);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>2)在Spring配置文件ApplicationContext.xml中进行配置</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">&lt;bean id&#x3D;&quot;globalConfiguration&quot; class&#x3D;&quot;com.baomidou.mybatisplus.entity.GlobalConfiguration&quot;&gt;</span><br><span class="line">        &lt;!--注入公共字段填充处理器--&gt;</span><br><span class="line">        &lt;property name&#x3D;&quot;metaObjectHandler&quot; ref&#x3D;&quot;myMetaObjectHandler&quot;&#x2F;&gt;</span><br><span class="line">&lt;&#x2F;bean&gt;</span><br><span class="line">&lt;!--公共字段填充处理器--&gt;</span><br><span class="line">&lt;bean id&#x3D;&quot;myMetaObjectHandler&quot; class&#x3D;&quot;org.example.MyMetaObjectHandler.MyMetaObjectHandler&quot;&#x2F;&gt;</span><br></pre></td></tr></table></figure>
<p>3)编写测试类</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">@Test</span><br><span class="line">    public <span class="keyword">void</span> <span class="function"><span class="title">testMetaObjectHandler</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">        User user=<span class="keyword">new</span> User();</span><br><span class="line">        <span class="comment">//user.setName(&quot;lucy&quot;);//不定义名字，自动进行填充</span></span><br><span class="line">        user.setLogicFlag(<span class="number">1</span>);</span><br><span class="line">        user.setId(<span class="number">5</span>);</span><br><span class="line">        userMapper.updateById(user);</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>
<p><strong>注意一个问题</strong>：如果更新操作您本不想更新那个字段这儿也会给您默认进行填充，所以使用的时候需要进行注意这个问题。</p>
<h1 id="十、Oracle主键Sequence"><a href="#十、Oracle主键Sequence" class="headerlink" title="十、Oracle主键Sequence"></a>十、Oracle主键Sequence</h1><p>MySQL：支持主键自增。IdType.Auto<br>Oracle：序列（Sequence）<br>1)实体类配置主键Sequence    @KeySequence(value=”序列名”,class=xxx.class逐渐属性类型)<br>2)全局MP主键生成策略为idType.INPUT<br>3)全局mp中配置Oracle主键Sequence<br>com.baomidou.mybatisplus.incrementer.OracleKeyGenerator<br>4)可以将@keySequence定义在父类中,可实现多个子类对应的多个表公用一个Sequence</p>
<h1 id="十一、IDEA插件EasyCode使用"><a href="#十一、IDEA插件EasyCode使用" class="headerlink" title="十一、IDEA插件EasyCode使用"></a>十一、IDEA插件EasyCode使用</h1><p>1.安装</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">点击File&gt;Settinds&gt;Plugins然后在Plugins里搜索easycode点击Install安装插件</span><br></pre></td></tr></table></figure>
<p><img src="https://img-blog.csdnimg.cn/20210205213156343.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQxMDQ5Mzcx,size_16,color_FFFFFF,t_70" alt="在这里插入图片描述"></p>
<p>2.搭建一个Maven的Web项目。生成之后需要手动创建在main下创Java目录并且创建包。这里创建包com.kevin（后面使用到）<br><img src="https://img-blog.csdnimg.cn/20210205213332141.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQxMDQ5Mzcx,size_16,color_FFFFFF,t_70" alt="在这里插入图片描述"><br>3.导入数据库<br><img src="https://img-blog.csdnimg.cn/20210205213632705.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQxMDQ5Mzcx,size_16,color_FFFFFF,t_70" alt="在这里插入图片描述"><br>4.配置数据库连接以及设置时区。<br><img src="https://img-blog.csdnimg.cn/20210205213735826.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQxMDQ5Mzcx,size_16,color_FFFFFF,t_70" alt="在这里插入图片描述"><br>点击Set time zone之后跳转到Advanced之后再点击回General<br><img src="https://img-blog.csdnimg.cn/20210205213852371.png" alt="在这里插入图片描述"><br>5.右键点击需要生成的数据库表<br><img src="https://img-blog.csdnimg.cn/20210205214050159.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQxMDQ5Mzcx,size_16,color_FFFFFF,t_70" alt="在这里插入图片描述"><br>6.选择所放置的包（com.java），确定路径正确之后一路点击yes就可以了<br><img src="https://img-blog.csdnimg.cn/20210205220542602.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQxMDQ5Mzcx,size_16,color_FFFFFF,t_70" alt="在这里插入图片描述"></p>
<p>7.完成之后结构图<br><img src="https://img-blog.csdnimg.cn/20210205220613453.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQxMDQ5Mzcx,size_16,color_FFFFFF,t_70" alt="在这里插入图片描述"></p>

                
            </div>
            <hr/>

            

    <div class="reprint" id="reprint-statement">
        
            <div class="reprint__author">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-user">
                        文章作者:
                    </i>
                </span>
                <span class="reprint-info">
                    <a href="/about" rel="external nofollow noreferrer">Kevin</a>
                </span>
            </div>
            <div class="reprint__type">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-link">
                        文章链接:
                    </i>
                </span>
                <span class="reprint-info">
                    <a href="http://example.com/2021/03/29/MyBatisPlus/">http://example.com/2021/03/29/MyBatisPlus/</a>
                </span>
            </div>
            <div class="reprint__notice">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-copyright">
                        版权声明:
                    </i>
                </span>
                <span class="reprint-info">
                    本博客所有文章除特別声明外，均采用
                    <a href="https://creativecommons.org/licenses/by/4.0/deed.zh" rel="external nofollow noreferrer" target="_blank">CC BY 4.0</a>
                    许可协议。转载请注明来源
                    <a href="/about" target="_blank">Kevin</a>
                    !
                </span>
            </div>
        
    </div>

    <script async defer>
      document.addEventListener("copy", function (e) {
        let toastHTML = '<span>复制成功，请遵循本文的转载规则</span><button class="btn-flat toast-action" onclick="navToReprintStatement()" style="font-size: smaller">查看</a>';
        M.toast({html: toastHTML})
      });

      function navToReprintStatement() {
        $("html, body").animate({scrollTop: $("#reprint-statement").offset().top - 80}, 800);
      }
    </script>



            <div class="tag_share" style="display: block;">
                <div class="post-meta__tag-list" style="display: inline-block;">
                    
                        <div class="article-tag">
                            <span class="chip bg-color">无标签</span>
                        </div>
                    
                </div>
                <div class="post_share" style="zoom: 80%; width: fit-content; display: inline-block; float: right; margin: -0.15rem 0;">
                    <link rel="stylesheet" type="text/css" href="/libs/share/css/share.min.css">
<div id="article-share">

    
    <div class="social-share" data-sites="twitter,facebook,google,qq,qzone,wechat,weibo,douban,linkedin" data-wechat-qrcode-helper="<p>微信扫一扫即可分享！</p>"></div>
    <script src="/libs/share/js/social-share.min.js"></script>
    

    

</div>

                </div>
            </div>
            
                <style>
    #reward {
        margin: 40px 0;
        text-align: center;
    }

    #reward .reward-link {
        font-size: 1.4rem;
        line-height: 38px;
    }

    #reward .btn-floating:hover {
        box-shadow: 0 6px 12px rgba(0, 0, 0, 0.2), 0 5px 15px rgba(0, 0, 0, 0.2);
    }

    #rewardModal {
        width: 320px;
        height: 350px;
    }

    #rewardModal .reward-title {
        margin: 15px auto;
        padding-bottom: 5px;
    }

    #rewardModal .modal-content {
        padding: 10px;
    }

    #rewardModal .close {
        position: absolute;
        right: 15px;
        top: 15px;
        color: rgba(0, 0, 0, 0.5);
        font-size: 1.3rem;
        line-height: 20px;
        cursor: pointer;
    }

    #rewardModal .close:hover {
        color: #ef5350;
        transform: scale(1.3);
        -moz-transform:scale(1.3);
        -webkit-transform:scale(1.3);
        -o-transform:scale(1.3);
    }

    #rewardModal .reward-tabs {
        margin: 0 auto;
        width: 210px;
    }

    .reward-tabs .tabs {
        height: 38px;
        margin: 10px auto;
        padding-left: 0;
    }

    .reward-content ul {
        padding-left: 0 !important;
    }

    .reward-tabs .tabs .tab {
        height: 38px;
        line-height: 38px;
    }

    .reward-tabs .tab a {
        color: #fff;
        background-color: #ccc;
    }

    .reward-tabs .tab a:hover {
        background-color: #ccc;
        color: #fff;
    }

    .reward-tabs .wechat-tab .active {
        color: #fff !important;
        background-color: #22AB38 !important;
    }

    .reward-tabs .alipay-tab .active {
        color: #fff !important;
        background-color: #019FE8 !important;
    }

    .reward-tabs .reward-img {
        width: 210px;
        height: 210px;
    }
</style>

<div id="reward">
    <a href="#rewardModal" class="reward-link modal-trigger btn-floating btn-medium waves-effect waves-light red">赏</a>

    <!-- Modal Structure -->
    <div id="rewardModal" class="modal">
        <div class="modal-content">
            <a class="close modal-close"><i class="fas fa-times"></i></a>
            <h4 class="reward-title">您的赏识是我前进的动力！</h4>
            <div class="reward-content">
                <div class="reward-tabs">
                    <ul class="tabs row">
                        <li class="tab col s6 alipay-tab waves-effect waves-light"><a href="#alipay">支付宝</a></li>
                        <li class="tab col s6 wechat-tab waves-effect waves-light"><a href="#wechat">微 信</a></li>
                    </ul>
                    <div id="alipay">
                        <img src="/medias/reward/alipay.jpg" class="reward-img" alt="支付宝打赏二维码">
                    </div>
                    <div id="wechat">
                        <img src="/medias/reward/wechat.png" class="reward-img" alt="微信打赏二维码">
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    $(function () {
        $('.tabs').tabs();
    });
</script>

            
        </div>
    </div>

    

    

    

    

    

    

    

    

    

<article id="prenext-posts" class="prev-next articles">
    <div class="row article-row">
        
        <div class="article col s12 m6" data-aos="fade-up">
            <div class="article-badge left-badge text-color">
                <i class="fas fa-chevron-left"></i>&nbsp;上一篇</div>
            <div class="card">
                <a href="/2021/03/29/Redis%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/">
                    <div class="card-image">
                        
                        
                        <img src="/medias/featureimages/9.jpg" class="responsive-img" alt="Redis学习笔记">
                        
                        <span class="card-title">Redis学习笔记</span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary block-with-text">
                        
                            
                        
                    </div>
                    <div class="publish-info">
                        <span class="publish-date">
                            <i class="far fa-clock fa-fw icon-date"></i>2021-03-29
                        </span>
                        <span class="publish-author">
                            
                            <i class="fas fa-user fa-fw"></i>
                            Kevin
                            
                        </span>
                    </div>
                </div>
                
            </div>
        </div>
        
        
        <div class="article col s12 m6" data-aos="fade-up">
            <div class="article-badge right-badge text-color">
                下一篇&nbsp;<i class="fas fa-chevron-right"></i>
            </div>
            <div class="card">
                <a href="/2021/03/10/MySql%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/">
                    <div class="card-image">
                        
                        
                        <img src="/medias/featureimages/3.jpg" class="responsive-img" alt="数据库学习笔记">
                        
                        <span class="card-title">数据库学习笔记</span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary block-with-text">
                        
                            
                        
                    </div>
                    <div class="publish-info">
                            <span class="publish-date">
                                <i class="far fa-clock fa-fw icon-date"></i>2021-03-10
                            </span>
                        <span class="publish-author">
                            
                            <i class="fas fa-user fa-fw"></i>
                            Kevin
                            
                        </span>
                    </div>
                </div>
                
            </div>
        </div>
        
    </div>
</article>

</div>



<!-- 代码块功能依赖 -->
<script type="text/javascript" src="/libs/codeBlock/codeBlockFuction.js"></script>

<!-- 代码语言 -->

<script type="text/javascript" src="/libs/codeBlock/codeLang.js"></script>


<!-- 代码块复制 -->

<script type="text/javascript" src="/libs/codeBlock/codeCopy.js"></script>


<!-- 代码块收缩 -->

<script type="text/javascript" src="/libs/codeBlock/codeShrink.js"></script>


    </div>
    <div id="toc-aside" class="expanded col l3 hide-on-med-and-down">
        <div class="toc-widget card" style="background-color: white;">
            <div class="toc-title"><i class="far fa-list-alt"></i>&nbsp;&nbsp;目录</div>
            <div id="toc-content"></div>
        </div>
    </div>
</div>

<!-- TOC 悬浮按钮. -->

<div id="floating-toc-btn" class="hide-on-med-and-down">
    <a class="btn-floating btn-large bg-color">
        <i class="fas fa-list-ul"></i>
    </a>
</div>


<script src="/libs/tocbot/tocbot.min.js"></script>
<script>
    $(function () {
        tocbot.init({
            tocSelector: '#toc-content',
            contentSelector: '#articleContent',
            headingsOffset: -($(window).height() * 0.4 - 45),
            collapseDepth: Number('0'),
            headingSelector: 'h2, h3, h4'
        });

        // modify the toc link href to support Chinese.
        let i = 0;
        let tocHeading = 'toc-heading-';
        $('#toc-content a').each(function () {
            $(this).attr('href', '#' + tocHeading + (++i));
        });

        // modify the heading title id to support Chinese.
        i = 0;
        $('#articleContent').children('h2, h3, h4').each(function () {
            $(this).attr('id', tocHeading + (++i));
        });

        // Set scroll toc fixed.
        let tocHeight = parseInt($(window).height() * 0.4 - 64);
        let $tocWidget = $('.toc-widget');
        $(window).scroll(function () {
            let scroll = $(window).scrollTop();
            /* add post toc fixed. */
            if (scroll > tocHeight) {
                $tocWidget.addClass('toc-fixed');
            } else {
                $tocWidget.removeClass('toc-fixed');
            }
        });

        
        /* 修复文章卡片 div 的宽度. */
        let fixPostCardWidth = function (srcId, targetId) {
            let srcDiv = $('#' + srcId);
            if (srcDiv.length === 0) {
                return;
            }

            let w = srcDiv.width();
            if (w >= 450) {
                w = w + 21;
            } else if (w >= 350 && w < 450) {
                w = w + 18;
            } else if (w >= 300 && w < 350) {
                w = w + 16;
            } else {
                w = w + 14;
            }
            $('#' + targetId).width(w);
        };

        // 切换TOC目录展开收缩的相关操作.
        const expandedClass = 'expanded';
        let $tocAside = $('#toc-aside');
        let $mainContent = $('#main-content');
        $('#floating-toc-btn .btn-floating').click(function () {
            if ($tocAside.hasClass(expandedClass)) {
                $tocAside.removeClass(expandedClass).hide();
                $mainContent.removeClass('l9');
            } else {
                $tocAside.addClass(expandedClass).show();
                $mainContent.addClass('l9');
            }
            fixPostCardWidth('artDetail', 'prenext-posts');
        });
        
    });
</script>

    

</main>




    <footer class="page-footer bg-color">
    
        <link rel="stylesheet" href="/libs/aplayer/APlayer.min.css">
<style>
    .aplayer .aplayer-lrc p {
        
        display: none;
        
        font-size: 12px;
        font-weight: 700;
        line-height: 16px !important;
    }

    .aplayer .aplayer-lrc p.aplayer-lrc-current {
        
        display: none;
        
        font-size: 15px;
        color: #42b983;
    }

    
    .aplayer.aplayer-fixed.aplayer-narrow .aplayer-body {
        left: -66px !important;
    }

    .aplayer.aplayer-fixed.aplayer-narrow .aplayer-body:hover {
        left: 0px !important;
    }

    
</style>
<div class="">
    
    <div class="row">
        <meting-js class="col l8 offset-l2 m10 offset-m1 s12"
                   server="netease"
                   type="playlist"
                   id="2039019608"
                   fixed='true'
                   autoplay='false'
                   theme='#42b983'
                   loop='all'
                   order='random'
                   preload='auto'
                   volume='0.7'
                   list-folded='true'
        >
        </meting-js>
    </div>
</div>

<script src="/libs/aplayer/APlayer.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/meting@2/dist/Meting.min.js"></script>

    
    <div class="container row center-align" style="margin-bottom: 15px !important;">
        <div class="col s12 m8 l8 copy-right">
            Copyright&nbsp;&copy;
            
                <span id="year">2021</span>
            
            <span id="year">2021</span>
            <a href="/about" target="_blank">Kevin</a>
            |&nbsp;Powered by&nbsp;<a href="https://hexo.io/" target="_blank">Hexo</a>
            |&nbsp;Theme&nbsp;<a href="https://github.com/blinkfox/hexo-theme-matery" target="_blank">Matery</a>
            <br>
            
            
            
            
            
            
            <span id="busuanzi_container_site_pv">
                |&nbsp;<i class="far fa-eye"></i>&nbsp;总访问量:&nbsp;<span id="busuanzi_value_site_pv"
                    class="white-color"></span>&nbsp;次
            </span>
            
            
            <span id="busuanzi_container_site_uv">
                |&nbsp;<i class="fas fa-users"></i>&nbsp;总访问人数:&nbsp;<span id="busuanzi_value_site_uv"
                    class="white-color"></span>&nbsp;人
            </span>
            
            <br>
            
            <span id="sitetime">载入运行时间...</span>
            <script>
                function siteTime() {
                    var seconds = 1000;
                    var minutes = seconds * 60;
                    var hours = minutes * 60;
                    var days = hours * 24;
                    var years = days * 365;
                    var today = new Date();
                    var startYear = "2021";
                    var startMonth = "3";
                    var startDate = "29";
                    var startHour = "0";
                    var startMinute = "0";
                    var startSecond = "0";
                    var todayYear = today.getFullYear();
                    var todayMonth = today.getMonth() + 1;
                    var todayDate = today.getDate();
                    var todayHour = today.getHours();
                    var todayMinute = today.getMinutes();
                    var todaySecond = today.getSeconds();
                    var t1 = Date.UTC(startYear, startMonth, startDate, startHour, startMinute, startSecond);
                    var t2 = Date.UTC(todayYear, todayMonth, todayDate, todayHour, todayMinute, todaySecond);
                    var diff = t2 - t1;
                    var diffYears = Math.floor(diff / years);
                    var diffDays = Math.floor((diff / days) - diffYears * 365);
                    var diffHours = Math.floor((diff - (diffYears * 365 + diffDays) * days) / hours);
                    var diffMinutes = Math.floor((diff - (diffYears * 365 + diffDays) * days - diffHours * hours) /
                        minutes);
                    var diffSeconds = Math.floor((diff - (diffYears * 365 + diffDays) * days - diffHours * hours -
                        diffMinutes * minutes) / seconds);
                    if (startYear == todayYear) {
                        document.getElementById("year").innerHTML = todayYear;
                        document.getElementById("sitetime").innerHTML = "本站已安全运行 " + diffDays + " 天 " + diffHours +
                            " 小时 " + diffMinutes + " 分钟 " + diffSeconds + " 秒";
                    } else {
                        document.getElementById("year").innerHTML = startYear + " - " + todayYear;
                        document.getElementById("sitetime").innerHTML = "本站已安全运行 " + diffYears + " 年 " + diffDays +
                            " 天 " + diffHours + " 小时 " + diffMinutes + " 分钟 " + diffSeconds + " 秒";
                    }
                }
                setInterval(siteTime, 1000);
            </script>
            
            <br>
            
        </div>
        <div class="col s12 m4 l4 social-link social-statis">
    <a href="https://github.com/ZKQevin" class="tooltipped" target="_blank" data-tooltip="访问我的GitHub" data-position="top" data-delay="50">
        <i class="fab fa-github"></i>
    </a>



    <a href="mailto:773116395@qq.com" class="tooltipped" target="_blank" data-tooltip="邮件联系我" data-position="top" data-delay="50">
        <i class="fas fa-envelope-open"></i>
    </a>







    <a href="tencent://AddContact/?fromId=50&fromSubId=1&subcmd=all&uin=773116395" class="tooltipped" target="_blank" data-tooltip="QQ联系我: 773116395" data-position="top" data-delay="50">
        <i class="fab fa-qq"></i>
    </a>







</div>
    </div>
</footer>

<div class="progress-bar"></div>


    <!-- 搜索遮罩框 -->
<div id="searchModal" class="modal">
    <div class="modal-content">
        <div class="search-header">
            <span class="title"><i class="fas fa-search"></i>&nbsp;&nbsp;搜索</span>
            <input type="search" id="searchInput" name="s" placeholder="请输入搜索的关键字"
                   class="search-input">
        </div>
        <div id="searchResult"></div>
    </div>
</div>

<script type="text/javascript">
$(function () {
    var searchFunc = function (path, search_id, content_id) {
        'use strict';
        $.ajax({
            url: path,
            dataType: "xml",
            success: function (xmlResponse) {
                // get the contents from search data
                var datas = $("entry", xmlResponse).map(function () {
                    return {
                        title: $("title", this).text(),
                        content: $("content", this).text(),
                        url: $("url", this).text()
                    };
                }).get();
                var $input = document.getElementById(search_id);
                var $resultContent = document.getElementById(content_id);
                $input.addEventListener('input', function () {
                    var str = '<ul class=\"search-result-list\">';
                    var keywords = this.value.trim().toLowerCase().split(/[\s\-]+/);
                    $resultContent.innerHTML = "";
                    if (this.value.trim().length <= 0) {
                        return;
                    }
                    // perform local searching
                    datas.forEach(function (data) {
                        var isMatch = true;
                        var data_title = data.title.trim().toLowerCase();
                        var data_content = data.content.trim().replace(/<[^>]+>/g, "").toLowerCase();
                        var data_url = data.url;
                        data_url = data_url.indexOf('/') === 0 ? data.url : '/' + data_url;
                        var index_title = -1;
                        var index_content = -1;
                        var first_occur = -1;
                        // only match artiles with not empty titles and contents
                        if (data_title !== '' && data_content !== '') {
                            keywords.forEach(function (keyword, i) {
                                index_title = data_title.indexOf(keyword);
                                index_content = data_content.indexOf(keyword);
                                if (index_title < 0 && index_content < 0) {
                                    isMatch = false;
                                } else {
                                    if (index_content < 0) {
                                        index_content = 0;
                                    }
                                    if (i === 0) {
                                        first_occur = index_content;
                                    }
                                }
                            });
                        }
                        // show search results
                        if (isMatch) {
                            str += "<li><a href='" + data_url + "' class='search-result-title'>" + data_title + "</a>";
                            var content = data.content.trim().replace(/<[^>]+>/g, "");
                            if (first_occur >= 0) {
                                // cut out 100 characters
                                var start = first_occur - 20;
                                var end = first_occur + 80;
                                if (start < 0) {
                                    start = 0;
                                }
                                if (start === 0) {
                                    end = 100;
                                }
                                if (end > content.length) {
                                    end = content.length;
                                }
                                var match_content = content.substr(start, end);
                                // highlight all keywords
                                keywords.forEach(function (keyword) {
                                    var regS = new RegExp(keyword, "gi");
                                    match_content = match_content.replace(regS, "<em class=\"search-keyword\">" + keyword + "</em>");
                                });

                                str += "<p class=\"search-result\">" + match_content + "...</p>"
                            }
                            str += "</li>";
                        }
                    });
                    str += "</ul>";
                    $resultContent.innerHTML = str;
                });
            }
        });
    };

    searchFunc('/search.xml', 'searchInput', 'searchResult');
});
</script>

    <!-- 回到顶部按钮 -->
<div id="backTop" class="top-scroll">
    <a class="btn-floating btn-large waves-effect waves-light" href="#!">
        <i class="fas fa-arrow-up"></i>
    </a>
</div>


    <script src="/libs/materialize/materialize.min.js"></script>
    <script src="/libs/masonry/masonry.pkgd.min.js"></script>
    <script src="/libs/aos/aos.js"></script>
    <script src="/libs/scrollprogress/scrollProgress.min.js"></script>
    <script src="/libs/lightGallery/js/lightgallery-all.min.js"></script>
    <script src="/js/matery.js"></script>

    <!-- Baidu Analytics -->

    <!-- Baidu Push -->

<script>
    (function () {
        var bp = document.createElement('script');
        var curProtocol = window.location.protocol.split(':')[0];
        if (curProtocol === 'https') {
            bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';
        } else {
            bp.src = 'http://push.zhanzhang.baidu.com/push.js';
        }
        var s = document.getElementsByTagName("script")[0];
        s.parentNode.insertBefore(bp, s);
    })();
</script>

    
    <script src="/libs/others/clicklove.js" async="async"></script>
    
    
    <script async src="/libs/others/busuanzi.pure.mini.js"></script>
    

    

    

    <!--腾讯兔小巢-->
    
    

    

    

    
    <script src="/libs/instantpage/instantpage.js" type="module"></script>
    

	<script src="https://cdn.jsdelivr.net/gh/Mculover666/live2d-widget/autoload.js"></script>
</body>

</html>
